package com.cg.managesystem.controller;

import com.cg.managesystem.common.JSONResult;
import com.cg.managesystem.entity.Repair;
import com.cg.managesystem.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created on 2019/8/13 by Tinchi
 **/
@RestController
@RequestMapping(value = "/manage")
public class RepairController {
    @Autowired
    private RepairService repairService;

    /**
     * 根据订单id查询保修订单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectByRepairId/{id}", method = RequestMethod.GET)
    public Repair selectByRepairId(@PathVariable("id") String id) {
        return repairService.selectByRepairId(id);
    }

    /**
     * 根据用户id查询保修订单
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/selectByUserId/{uid}", method = RequestMethod.GET)
    public Repair selectByUserId(@PathVariable("uid") Integer uid) {
        return repairService.selectByUserId(uid);
    }

    /**
     * 根据维修工人查询报修订单
     *
     * @param worker
     * @return
     */
    @RequestMapping(value = "/selectByWorkId/{worker}", method = RequestMethod.GET)
    public List<Repair> selectByWorkerId(@PathVariable("worker") String worker) {
        return repairService.selectByWorkerId(worker);
    }

    /**
     * 根据时间查询保修信息
     *
     * @param time
     * @return
     */
    @RequestMapping(value = "/selectByStarttime/{time}", method = RequestMethod.GET)
    public List<Repair> selectByStarttime(@PathVariable("time") String time) {
        String starttime = "";
        String endtime = "";
        //  查询出开始当天全部保修信息
        if (time.length() == 8) {
            starttime = time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + "%";
            return repairService.selectByStarttime(starttime);
        }
        return null;
    }

    /**
     * 查询所有报修信息的方法
     *
     * @return Repairs
     */
    @PostMapping(value = "/findAllRepairs")
    public List<Repair> findAllRepairs() {
        return repairService.findAllRepairs();
    }

    /**
     * 新增一个报修信息，需传入两个参数
     *
     * @param problem，uid
     * @return 成功插入数据库返回1
     */
    @PostMapping(value = "/addRepair")
    public JSONResult addRepair(Repair repair,
                                @PathVariable String problem,
                                @PathVariable Integer uid
    ) {
        repair.setProblem(problem);
        repair.setUid(uid);
        repair.setWorker("张三");
        repair.setStarttime(new Date());
        repair.setStatus("0");

        int result = repairService.addRepair(repair);
        if (result != 0) {
            return JSONResult.ok("提交保修成功");
        } else {
            return JSONResult.errorMsg("提交保修失败");
        }
    }

    /**
     * 按ID来删除报修信息
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/deleteRepairById")
    public int deleteRepairById(String id) {
        return repairService.deleteRepairById(id);
    }

    /**
     * 按ID更新报修信息
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/updateRepairById")
    public int updateRepairById(Repair record) {
        return repairService.updateRepairById(record);
    }
}
